package com.dr.cmdb.core.controller;

import com.dr.cmdb.common.model.response.ApiResponse;
import com.dr.cmdb.core.annotation.AuditLog;
import com.dr.cmdb.core.annotation.ModelCheck;
import com.dr.cmdb.core.enums.FiledCheckEnum;
import com.dr.cmdb.core.annotation.MongoIdConvert;
import com.dr.cmdb.core.enums.OperateTypeEnum;
import com.dr.cmdb.core.service.CiItemService;
import com.dr.cmdb.common.utils.excel.ExcelUtil;
import com.dr.cmdb.facade.api.ModelApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author rui.duan
 * @version 1.0
 * @className ModelController
 * @description 配置项管理
 * @date 2019/11/20 6:25 下午
 */
@RestController
@Api(value = "/api/model", tags = "配置项管理")
@RequestMapping("/api/model")
public class ModelController implements ModelApi {

    @Autowired
    private CiItemService ciItemService;

    @Override
    @ApiOperation("查询列表")
    @GetMapping("/list")
    @MongoIdConvert
    public ApiResponse<List<Object>> getList(
            @ApiParam(required = true, name = "searchParams", value = "查询参数")
            @RequestParam Map<String, Object> searchParams) {
        return new ApiResponse<List<Object>>().success(ciItemService.getListByParams(searchParams));

    }

    @Override
    @ApiOperation("添加ci项")
    @PostMapping("/add")
    @AuditLog(operateModule = "资源",operateType = OperateTypeEnum.ADD)
    @ModelCheck(value = {FiledCheckEnum.CHECK_FILED_EXIST, FiledCheckEnum.CHECK_FILED_NULL})
    public ApiResponse add(
            @ApiParam(required = true, name = "params", value = "添加参数")
            @RequestBody Map<String, Object> params) {
        ciItemService.addCiItem(params);
        return new ApiResponse<>().success();
    }

    @Override
    @ApiOperation("修改ci项")
    @PostMapping("/edit")
    public ApiResponse edit(
            @ApiParam(required = true, name = "params", value = "修改参数")
            @RequestBody Map<String, Object> params) {
        ciItemService.editCiItem(params);
        return new ApiResponse<>().success();

    }

    @Override
    @ApiOperation("删除ci项")
    @PostMapping("/delete/{code}")
    public ApiResponse delete(@ApiParam(required = true, name = "code", value = "ci项id")
                              @PathVariable("code") String code) {
        ciItemService.deleteCiItem(code);
        return new ApiResponse<>().success();

    }

    @Override
    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response, @ApiParam(required = true, name = "searchParams", value = "查询参数")
    @RequestParam Map<String, Object> searchParams) {
        ciItemService.exportCiItems(searchParams, "ci配置项", "ci配置项", response);
    }

    @Override
    @ApiOperation("导入数据")
    @PostMapping("/import")
    public ApiResponse importExcel(@ApiParam(required = true, name = "file", value = "导入文件对象")
                                   @RequestParam(name = "file") MultipartFile file,
                                   @ApiParam(required = true, name = "tableName", value = "导入表名")
                                   @RequestParam(name = "tableName") String tableName
    ) {
        List<Map<String, Object>> maps = ExcelUtil.importExcel(file, 0, 1);
        ciItemService.importCiItems(maps, tableName);
        return new ApiResponse().success();
    }

}
